<?php

namespace eu\freeplace\core;

/**
 * Generic class for compressing website sourcecode.
 *
 * This class requires PHP 5.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * @author Simon Wippich <development@wippich.org>
 * @copyright Copyright (c) 2009, Simon Wippich
 * @license http://www.gnu.org/licenses GNU Lesser General Public License
 * @version 1.0
 */
class CodeCompressor {

    const CLASS_NAME = "CodeCompressor";

    public function __construct() {
      global $logger;
      $logger->log("Calling instance of ".self::CLASS_NAME."()", "NOTICE");
    }
    
    /**
     * Method for compressing HTML-sourcecode.
     *
     * @access public
     * @param $htmlSourceCode String The original HTML-sourcecode
     * @return String The compressed HTML-sourcecode
     */
    final public static function compressHtml($htmlSourceCode, $removeComments=true) {
        global $logger;

        // Initialize a returning variable
        $returnValue = '';
        try {
            // Check if the given parameter matches datatype string
            if (is_string($htmlSourceCode)) {
                // Exclude pre- or code-tags
                preg_match_all(
                        '!(<(?:code|pre).*>[^<]+</(?:code|pre)>)!', $htmlSourceCode, $pre
                );
                // Remove all pre- or code-tags
                $htmlSourceCode = preg_replace(
                        '!<(?:code|pre).*>[^<]+</(?:code|pre)>!', '#pre#', $htmlSourceCode
                );
                // Remove HTML-comments if required
                if ($removeComments === true) {
                    $htmlSourceCode = preg_replace(
                        '/<!--(.|\s)*?-->/', '', $htmlSourceCode
                    );
                }
                // Remove new lines, spaces and tabs
                $htmlSourceCode = preg_replace(
                        '/[\r\n\t]+/', ' ', $htmlSourceCode
                );
                $htmlSourceCode = preg_replace(
                        '/>[\s]+</', '><', $htmlSourceCode
                );
                $htmlSourceCode = preg_replace(
                        '/[\s]+/', ' ', $htmlSourceCode
                );
                if (!empty($pre[0])) {
                    foreach ($pre[0] as $tag) {
                        // Return pre- and code-tags
                        $htmlSourceCode = preg_replace(
                                '!#pre#!', $tag, $htmlSourceCode, 1
                        );
                    }
                }
                // Remove preceding and trailing spaces and write
                // the processed sourcecode into the returning variable
                $returnValue = trim($htmlSourceCode);
                // Delete the original sourcecode
                unset($htmlSourceCode);

            } else {
                // Throw an exception in case of an invalid method parameter
                throw new Exception(
                        'Method "' .
                        __FUNCTION__ .
                        '" of class "CodeCompressor" reported an error: ' .
                        'Invalid parameter!'
                );
            }

        } catch (Exception $e) {
            // Catch all occuring exceptions form the preceding codeblock
            // and write the errormessages into the returning variable
            $logger->log($e->getMessage(), "ERROR");
        }

        // Read the Configuration
        if($config->core->htmlOutShrink['enable'] == true) {
            $html = $returnValue;
            $msg  = " - ENABLE";
        } else {
            $html = $htmlSourceCode;
            $msg = " - FALSE";
        }
        
        $logger->log(self::CLASS_NAME."().compressHtml()".msg, "DEBUG");
        return $html;
    }

}//CLASS

?>